<?php

declare(strict_types=1);

namespace app\wechat;

use think\facade\Event;

/**
 * Wechat应用全局注册 实现跨模块调用
 */
class Wechat
{
    // 应用全局注册
    public function boot()
    {
        $this->appRegister();
        $this->eventRegister();
    }

    // 注册微信公众号相关服务
    protected function appRegister()
    {
        $wechat = config('wechat');

        app()->bind("wechat.official_account", function () use ($wechat) {
            $officailAccount = sheep_config('wechat.officialAccount');
            $config = array_merge($wechat['defaults'], $officailAccount);
            $app = new \EasyWeChat\OfficialAccount\Application($config);
            if ($config['use_think_cache']) {
                $app['cache'] = cache();
            }
            return $app;
        });
    }

    // 注册微信相关事件监听
    protected function eventRegister()
    {
        Event::subscribe('app\wechat\subscribe\OfficialAccount');
    }
}
